package com.cloud.mall.pms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.cms.out.CmsSubjectOut;
import com.cloud.mall.constants.Constants;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.pms.dao.HomeDao;
import com.cloud.mall.pms.dto.home.*;
import com.cloud.mall.pms.fegin.CmsSubjectFeign;
import com.cloud.mall.pms.fegin.SmsFlashPromotionFegin;
import com.cloud.mall.pms.fegin.SmsHomeAdvertiseFegin;
import com.cloud.mall.pms.mapper.PmsProductCategoryMapper;
import com.cloud.mall.pms.mapper.PmsProductMapper;
import com.cloud.mall.pms.model.PmsProduct;
import com.cloud.mall.pms.model.PmsProductCategory;
import com.cloud.mall.pms.model.PmsProductCategoryExample;
import com.cloud.mall.pms.model.PmsProductExample;
import com.cloud.mall.pms.out.PmsProductCategoryOut;
import com.cloud.mall.pms.out.PmsProductOut;
import com.cloud.mall.pms.service.HomeService;
import com.cloud.mall.sms.out.SmsHomeAdvertiseOut;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class HomeServiceImpl implements HomeService {
    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;
    @Autowired
    private HomeDao homeDao;
    @Autowired
    private CmsSubjectFeign cmsSubjectFeign;
    @Autowired
    private SmsHomeAdvertiseFegin smsHomeAdvertiseFegin;
    @Autowired
    private SmsFlashPromotionFegin smsFlashPromotionFegin;


    @Override
    public BaseResponse<HomeContentResult> content() {
        HomeContentResult result = new HomeContentResult();
        BaseResponse<List<CmsSubjectOut>> subjectResp = cmsSubjectFeign.getRecommendSubjectList(0, 4);
        if (!subjectResp.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            return BaseResponse.setResultError(subjectResp.getMsg());
        }
        BaseResponse<List<SmsHomeAdvertiseOut>> homeAdvertiseResp = smsHomeAdvertiseFegin.getHomeAdvertiseList();
        if (!homeAdvertiseResp.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            return BaseResponse.setResultError(homeAdvertiseResp.getMsg());
        }
        BaseResponse<com.cloud.mall.sms.dto.product.HomeFlashPromotion> homeFlashPromotionResp = smsFlashPromotionFegin.getHomeFlashPromotion();
        if (homeFlashPromotionResp.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            //获取秒杀信息
           // result.setHomeFlashPromotion(MallBeanUtils.copyDoToDto(homeFlashPromotionResp.getData(),HomeFlashPromotion.class));
        }
        //获取首页广告
        result.setAdvertiseList(MallBeanUtils.copyDoToDto(homeAdvertiseResp.getData(), SmsHomeAdvertise.class));
        //获取推荐品牌
        result.setBrandList(MallBeanUtils.copyDoToDto(homeDao.getRecommendBrandList(0,4), PmsBrand.class));
        //获取新品推荐
        result.setNewProductList(MallBeanUtils.copyDoToDto(homeDao.getNewProductList(0,4), com.cloud.mall.pms.dto.home.PmsProduct.class));
        //获取人气推荐
        result.setHotProductList(MallBeanUtils.copyDoToDto(homeDao.getHotProductList(0,4), com.cloud.mall.pms.dto.home.PmsProduct.class));
        //获取推荐专题
        result.setSubjectList(MallBeanUtils.copyDoToDto(subjectResp.getData(), CmsSubject.class));
        return BaseResponse.setResultSuccess(result);
    }

    @Override
    public BaseResponse<CommonPage<PmsProductOut>> recommendProductList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PmsProductExample example = new PmsProductExample();
        example.createCriteria()
                .andDeleteStatusEqualTo(0)
                .andPublishStatusEqualTo(1);
        List<PmsProduct> pmsProducts = productMapper.selectByExample(example);
        return  BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(pmsProducts,PmsProductOut.class)));

    }

    @Override
    public BaseResponse<List<PmsProductCategoryOut>> getProductCateList(Long parentId) {
        PmsProductCategoryExample example = new PmsProductCategoryExample();
        example.createCriteria()
                .andShowStatusEqualTo(1)
                .andParentIdEqualTo(parentId);
        example.setOrderByClause("sort desc");
        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectByExample(example);
        return  BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(pmsProductCategories,PmsProductCategoryOut.class));
    }

}
